#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<cstring>
#include<vector>
using namespace std;
const int N = 10010;
int h[N], e[N], ne[N], idx;
int f[N];
vector<int> v;
int n;
void add(int a, int b)
{
    e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}
int dfs(int u)
{
    if (f[u] > 0) return f[u];
    f[u] = 1;
    for (int i = h[u]; ~i; i = ne[i])
    {
        int j = e[i];
        dfs(j);
        f[u] = max(f[u], f[j] + 1);
    }
    return f[u];
}
void find(int u)
{
    if (f[u] <= 1 || h[u] == -1) return;
    int mt = e[h[u]];
    for (int i = h[u]; ~i; i = ne[i])
    {
        int j = e[i];
        if (f[j] > f[mt] || f[j] == f[mt] && j < mt) mt = j;
    }
    v.push_back(mt);
    find(mt);
}
int main()
{
    memset(h, -1, sizeof h);
    memset(f, -1, sizeof f);
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        int k;
        cin >> k;
        for (int j = 0; j < k; j++)
        {
            int x;
            cin >> x;
            add(i, x);
        }
    }
    for (int i = 0; i < n; i++) dfs(i);
    int ansi = 0;
    for (int i = 0; i < n; i++) if (f[i] > f[ansi]) ansi = i;
    cout << f[ansi] << endl;
    v.push_back(ansi);
    find(ansi);
    for (int i = 0; i < v.size() - 1; i++) cout << v[i] << " ";
    cout << v.back();
    return 0;
}